﻿using PLCS.Application.Contracts.HisTaskApp.Dtos;
using PLCS.Application.Contracts.Params;
using Prism.Commands;

namespace PLCS.App.OrderTask.ViewModels.HisTask;

public class HisTaskViewModel : NavigationPagedCurdViewModel<HisTaskDto, HisTaskDto, IHisTaskService>
{
    private string _startTime = string.Empty;

    public string StartTime
    {
        get { return _startTime; }
        set { _startTime = value; RaisePropertyChanged(); }
    }

    private string _endTime = string.Empty;

    public string EndTime
    {
        get { return _endTime; }
        set { _endTime = value; RaisePropertyChanged(); }
    }

    private string _taskId;

    public string TaskId
    {
        get { return _taskId; }
        set { SetProperty(ref _taskId, value); }
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <param name="pageIndex"></param>
    public override async Task GetPageData(int pageIndex)
    {
        try
        {
            UpdateLoading(true);
            var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, TaskId, StartTime, EndTime);
            TotalCount = (int)res.TotalCount;
            SetPageCount();
            DataGridList = res.Items.ToObservable();
        }
        finally
        {
            UpdateLoading(false);
        }
    }

    public DelegateCommand<HisTaskDto> AgainIssuedCommand { get; set; }

    public HisTaskViewModel(IHisTaskService service) : base(service)
    {
        AgainIssuedCommand = new DelegateCommand<HisTaskDto>(AgainIssued);
    }

    private async void AgainIssued(HisTaskDto hisTask)
    {
        try
        {
            UpdateLoading(true);
            await _service.AgainIssuedAsync(hisTask);
            await RefreshAsync();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
        finally { UpdateLoading(false); }
    }
}